<?php

/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 9/8/2016
 * Des: 日志
 */
class Log
{
    private $path = LOG_PATH;

    public function __construct($path = '')
    {
        $this->path = rtrim($this->path . trim($path, '/'), '/') . SEPARATE;
        if (!is_dir($this->path)) {
            if (!makeDir($this->path, 0755, true)) {
                LD("Make dir: $this->path failed !");
            }
        }
    }

    private function level($level)
    {
        switch ($level) {
            case '1':
                return 'Notice:';
                break;
            case '2':
                return 'Error:';
                break;
            case '3':
                return 'Warming:';
                break;
            default:
                return 'Error:';
                break;
        }
    }

    public function write($msg, $level = 2, $file_name = '')
    {
        if ($msg == '') return false;
        $date = '[' . date("Y-m-d H:i:s", time()) . ']';
        $level = $this->level($level);
        if ($file_name == "") {
            $file = $this->path . date("Ymd", time()) . ".log";
        } else {
            $file = $this->path . $file_name;
        }
        if (is_file($file)) {
            file_put_contents($file, $date . $level . $msg . PHP_EOL, FILE_APPEND);
        } else {
            file_put_contents($file, $date . $level . $msg . PHP_EOL);
        }
    }

    public function debug_write($msg, $level = 2)
    {
        $this->write($msg, $level);
    }
}